package com.work.service.impl;

import com.work.bean.Hw;
import com.work.bean.HwAttach;
import com.work.bean.Teacher;
import com.work.dao.HwAttachDao;
import com.work.dao.HwDao;
import com.work.dao.TeacherDao;
import com.work.dao.impl.HwAttachDaoImpl;
import com.work.dao.impl.HwDaoImpl;
import com.work.dao.impl.TeacherDaoImpl;
import com.work.dto.TeacherHw;
import com.work.service.HwService;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class HwServiceImpl implements HwService {
    HwDao hwDao = new HwDaoImpl();
    TeacherDao teacherDao = new TeacherDaoImpl();
    HwAttachDao hwAttachDao = new HwAttachDaoImpl();
    public List<Hw> queryHw() { return hwDao.queryHw(); }

    public Hw queryHwById(Integer id) { return hwDao.queryHwById(id); }

    public List<TeacherHw> queryTeacherHw() {
        List<TeacherHw> list = new ArrayList<TeacherHw>();
        List<Hw> hw = hwDao.queryHw();
        for (Hw value : hw) {
            TeacherHw teacherHw = new TeacherHw(value.getId(), teacherDao.queryTeacherById(value.getTeacher_id()).getId(),teacherDao.queryTeacherById(value.getTeacher_id()).getName(), value.getTitle(), value.getDescribe(), value.getEnd_time());
            list.add(teacherHw);
        }
        return list;
    }

    public int saveHw(Hw hw,Teacher teacher,String url) {
        hwDao.saveHw(hw);
        int hw_id = hwDao.queryHwByTime().getId();
        hwAttachDao.saveHwAttach(new HwAttach(null,hw_id,hw.getTitle(),"0",url,new Date(),teacher.getName()));
        return 0;
    }

    public int updateHw(Hw hw) { return hwDao.updateHw(hw); }

    public int deleteHw(Integer id) { return hwDao.deleteHw(id); }
}